<?php
/**
 * Init all config to run website
 *
 * @author HAU DAI
 */
configureDebug(
    //Add more ip you want to run in debug mode below
    array('127.0.0.1',$_SERVER['REMOTE_ADDR'])
);

loadFramework($yiiPath);
configureEnvironment();

////////////////////////////// Supporting functions /////////////////////////////////
/**
 * Configure the application to run in debug mode or not.
 * If the site run on localhost it will automatically run on debug mode
 *
 * @param array devIPs an array of IPs that will run the app in debug mode
 * @return void
 *
 * @author HAU DAI
 */
function configureDebug($ips = array('127.0.0.1'))
{
    if (in_array($_SERVER['REMOTE_ADDR'], $ips))
        define("YII_DEBUG", true);

    defined('YII_DEBUG') or define('YII_DEBUG', false);

    if (YII_DEBUG) {
        set_time_limit(0);
        error_reporting(E_ALL);
        ini_set('display_errors', 1);
        define('YII_TRACE_LEVEL', 10);
    }
}

/**
 * Load Yii framework
 * Framework path is configured in index.php
 *
 * @author HAU DAI
 */
function loadFramework($yiiPath)
{
    if (is_file($yiiPath))
        require_once($yiiPath);
    else {
        echo '<pre>', '
There is an error trying to load the framework at
' . $yiiPath . '
Edit the init.php to correct path to Yii framework.
        ', '</pre>';
        die;
    }
}

/**
 * Load the environment file and Yii config file for the application
 * The Yii config file must be named as main-APP_ID where APP_ID is defined in the index.php
 *
 * @return void
 *
 * @author HAU DAI
 */
function configureEnvironment()
{
    if (!defined('APP_ID'))
        die('APP_ID is not defined in' . $_SERVER['SCRIPT_FILENAME']);

    $request = new CHttpRequest();
    $hostInfo = parse_url($request->getHostInfo(), PHP_URL_HOST);
////////////////////////////Use for ssl request
//    // Handle SSL requirement
//    defined('REQUIRE_SSL') or define('REQUIRE_SSL', false);
//    if (isSet($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
//       $_SERVER['HTTPS'] = 'on';
//    }
//    if (REQUIRE_SSL && $_SERVER['HTTPS'] == false){
//       header('location:' . str_replace('http', 'https', $request->getHostInfo() . $request->requestUri));
//       die;
//    }
/////////////////////////////////////////////////////////////////////////////////////////////////////////
    $GLOBALS['APP_ID'] = APP_ID;
    $appConfig = null;

    // environment file locates in /sites/env folder using this naming convention: config.domain.php
    $envFile = dirname(__FILE__) . '/sites/env/config.' . $hostInfo . '.php';
    if (file_exists($envFile))
        include($envFile);
    else
        die('Environment file is not existed');

    // support single login to manage different sub domain
    $GLOBALS['TLD'] = '.' . implode('.', array_slice(explode('.', $hostInfo), -2));
    // load application config
    $appConfig = dirname(__FILE__) . '/protected/config/main-' . $GLOBALS['APP_ID'] . '.php';
    if (file_exists($appConfig))
        $appConfig = include($appConfig);
    else
        die('Config file is not existed');

    // merge with base config
    $base = require(dirname(__FILE__) . '/protected/config/base.php');
    $GLOBALS['config'] = CMap::mergeArray($base, $appConfig);
}